package com.plian.system.service.sys.per;

import com.plian.Tools.TreeUtli;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.per.FunGroupDao;
import com.plian.system.mapper.sys.fun.SysFunMapper;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.node.ForestNodeMerger;
import com.plian.system.vo.sys.fun.SysFunVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class FunGroupService {
    @Autowired
    FunGroupDao funGroupDao;
    @Resource
    SysFunMapper sysFunMapper;
    /**
     * 配置权限组-权限
     * @param pd
     * @return
     */
    @Transactional(rollbackFor=RuntimeException.class)
    public ObjResult PerGroupconfig(PageData pd){
        //功能id集合
        List<String> ids= (List<String>) pd.get("funIds");
        //权限组id
        String groupId=pd.getString("groupId");
        List<PageData> list=new ArrayList<>();
        ObjResult objResult;
        ids.forEach(s -> {
            PageData p=new PageData();
            p.put("groupId",groupId);
            p.put("id", UuidUtil.get32UUID());
            p.put("funId",s);
            list.add(p);
        });
        try {
            funGroupDao.del_FunGroup(groupId);
            funGroupDao.FunGroupConfig(list);
            objResult=new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.ERROR));
        }
        return objResult;
    }

    /**
     * 选择当前账号下，权限单位树结构
     * @param pd
     * @return
     * @throws Exception
     */
    public ListResult<SysFunVO> queryFunGroup(PageData pd)  {
        ListResult listResult;
        try {
            List<PageData> list= funGroupDao.queryFunGroup(pd.getString("groupId"));
            List<SysFunVO> FunAll = sysFunMapper.list(null, null, null);
            //循环将有权限的标识
            FunAll.forEach(sysFunVO -> {
                sysFunVO.setCheck(false);
                list.forEach(p ->{
                    if(sysFunVO.getId().equals(p.getString("funId"))){
                        sysFunVO.setCheck(true);
                    }
                });
            });
            TreeUtli treeUtli=new TreeUtli();
            List<SysFunVO> merge = ForestNodeMerger.merge(FunAll);
            listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(merge);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException(new ObjResult(CommonCode.ERROR));
        }
        return listResult;
    }
}
